
import dayjs from 'dayjs'

import { cloneDeep } from '@renderer/utils/utils'
/**
 * 根据表单的数据获取新增所需要的接口
 * @param formData 
 */
export const getCreateParamsByFormData = (data, { isEdit, isGroup }) => {
    const formData = cloneDeep(data)

    // 处理关键词的选择
    if (formData.keyword.type === 1) {
        formData.keyword.fileCode = 0
    } else {
        formData.keyword.groups = []
    }
    formData.keyword.fileInfo = undefined

    // 处理选择客户标签
    if (formData.custFilter.type === 1) {
        formData.custFilter.filterType = 1
        formData.custFilter.tags = []
    } else {
        formData.custFilter.tags = formData.custFilter.tags.map(item => item.Id || item.id)
    }

    // 处理群聊标签
    if (formData.custGroupFilter.type === 1) {
        formData.custGroupFilter.chatId = []
    } else {
        formData.custGroupFilter.chatId = formData.custGroupFilter.chatId.map(item => item.id)
    }

    // 处理话术的文件信息
    formData.speechCraft.forEach(speechcraftItem => {
        speechcraftItem.attachments.forEach(attItem => {
            attItem.fileInfo = undefined
        })
    })

    // 处理重发限制
    if (formData.replayLimitSwitch) {
        formData.replayLimit = isGroup ? formData.replayLimit : formData.replayLimit * 60
    } else {
        formData.replayLimit = 0
    }
    formData.replayLimitSwitch = undefined

    // 处理时间
    if (formData.enableTime.type === 1) {
        formData.enableTime.time = []
    }
    formData.enableTime.time.forEach((times) => {
        const [beginTime, endTime] = times.time
        times.beginTime = dayjs(beginTime).format('HH:mm:ss')
        times.endTime = dayjs(endTime).format('HH:mm:ss')
        // times.time = undefined
    })

    // 如果是私聊的情况，去掉群聊的接口的参数
    if (!isGroup) {
        formData.altEnable = undefined
        formData.custGroupFilter = undefined
    } else {
        // 群聊去掉私聊的，
        formData.custFilter = undefined
    }

    return formData
}

/**
 * 合并数据到formData
 * @param formData 页面表单数据
 * @param data 接口返回的数据
 */
export const handleMergeFormData = (formData, data, { isGroup }) => {
    data.enableTime.time.forEach((times) => {
        const { beginTime, endTime } = times
        // 把日期改成当天,原因是element时间选择组件会默认今天，并且月份不是从0开始所以+1
        const now = dayjs()
        const newYear = now.get('year')
        const nowMonth = now.get('month')
        const nowDateTime = now.get('date')

        const b = new Date(`${newYear}-${nowMonth + 1}-${nowDateTime} ${dayjs(beginTime).format('HH:mm:ss')}`)
        const e = new Date(`${newYear}-${nowMonth + 1}-${nowDateTime} ${dayjs(endTime).format('HH:mm:ss')}`)
        times.time = [b, e]
    })

    // 处理重发限制 || 防骚扰限制
    data.replayLimit = isGroup ? data.replayLimit : data.replayLimit / 60
    data.replayLimitSwitch = Boolean(data.replayLimit)

    // 处理标签筛选
    data.custFilter?.tags?.forEach(item => {
        item.Id = item.id
        item.Name = item.name
    })

    // 处理群聊列表的字段
    if (isGroup) {
        // 把群聊的转换成首字母大写
        data.custGroupFilter.chatId.forEach(chatItem => {
            for (const [key, value] of Object.entries(chatItem)) {
                const upperCaseKey = key.charAt(0).toUpperCase() + key.slice(1);
                chatItem[upperCaseKey] = value
            }
        })
    }

    Object.assign(formData, data)
}